package org.mclll.xwangplugin.questTracking.questGoal;

import me.pikamug.quests.events.quester.BukkitQuesterPostUpdateObjectiveEvent;
import me.pikamug.quests.quests.components.BukkitObjective;
import me.pikamug.quests.quests.components.Stage;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.scoreboard.Objective;
import org.jetbrains.annotations.NotNull;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;

import java.util.List;

/**
 * @author: geduo
 * @date: 2023/10/13 16:02
 */
public class CatchFishQuestTrack {
    private static final String LINE_PREFIX = ChatColor.YELLOW + "捕鱼";
    private static final String LINE_FORMAT = LINE_PREFIX + " (%d/%d)";
    public static void questTracking(Objective context, Integer line, Stage questsStage) {
        Integer cowsToMilk = questsStage.getFishToCatch();
        if (cowsToMilk == null || cowsToMilk == 0) {
            return;
        }
        context.getScore(getGoalFormat(0, cowsToMilk)).setScore(line--);
    }

    public static String getGoalFormat(Integer progress, Integer cowsToMilk) {
        return String.format(LINE_FORMAT, progress, cowsToMilk);
    }
    @NotNull
    public static String getFinishFormat() {
        return ChatColor.GRAY + "捕鱼 (✓)";
    }

    public static void goalFinish(BukkitQuesterPostUpdateObjectiveEvent event, List<Persistence.SerializeScore> scoreboard) {
        for (Persistence.SerializeScore score : scoreboard) {
            String entry = score.getName();
            if (entry.startsWith(LINE_PREFIX)) {
                BukkitObjective questObj = (BukkitObjective) event.getObjective();
                int total = questObj.getGoal() - questObj.getProgress();
                if (total == 0) {
                    score.setName(getFinishFormat());
                    score.setValue(scoreboard.get(scoreboard.size() - 1).getValue() - 1);
                } else {
                    score.setName(String.format(LINE_FORMAT, questObj.getProgress(), questObj.getGoal()));
                }
            }
        }
    }

}
